package com.lucene.blog.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.lucene.blog.bean.BlogType;
import com.lucene.blog.bean.PageBean;
import com.lucene.blog.db.service.BlogService;
import com.lucene.blog.db.service.BlogTypeService;
import com.lucene.blog.util.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/admin/blogType")
public class BlogTypeAdminController {

	@Resource
	private BlogTypeService blogTypeService;
	@Resource
	private BlogService blogService;
	
	/**
	 * 分页查询博客类别
	 * @param page
	 * @param rows
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listBlogType")
	public String listBlogType(@RequestParam(value="page", required=false) String page,
							   @RequestParam(value="rows", required=false) String rows,
							   HttpServletResponse response) throws Exception {
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("pageBegin", pageBean.getPageBegin());
		condition.put("pageSize", pageBean.getPageSize());
		List<BlogType> blogTypeList = blogTypeService.getBlogTypeList(condition);
		Long total = blogTypeService.getTotal(condition);
		
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(blogTypeList);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	@RequestMapping("/listAllBlogType")
	public String listAllBlogType(HttpServletResponse response) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		List<BlogType> blogTypeList = blogTypeService.getBlogTypeList(condition);
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("blogTypeList", blogTypeList);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 添加或修改博客类别
	 * @param blogType
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(BlogType blogType, HttpServletResponse response) throws Exception {
		int resultTotal = 0;
		if(blogType.getBlogTypeId() == null) {
			resultTotal = blogTypeService.addBlogType(blogType);
		} else {
			resultTotal = blogTypeService.updateBlogType(blogType);
		}
		
		JSONObject result = new JSONObject();
		if(resultTotal > 0) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 删除博客类别信息
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String deleteBlogType(@RequestParam(value="ids", required=false) String ids,
								 HttpServletResponse response) throws Exception {
		String[] idsStr = ids.split(",");
		JSONObject result = new JSONObject(); 
		for(int i = 0; i < idsStr.length; i++) {
			int blogTypeId = Integer.parseInt(idsStr[i]);
			if(blogService.getBlogCountByTypeId(blogTypeId) > 0) {
				result.put("exist", "该类别下有博客，不能删除!");
			} else {
				blogTypeService.deleteBlogTypeById(blogTypeId);
			}
		}
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}
	
	
}
